package com.crm.system.service.impl.cluePoolManager;

import com.alibaba.fastjson.JSONObject;
import com.crm.common.model.Enum.StatusCodeEnum;
import com.crm.common.util.CommonUtils;
import com.crm.dao.mapper.cluePoolManager.CluePoolDistributionMapper;
import com.crm.model.entity.cluePoolManager.CluePoolDistribution;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.cluePoolManager.CluePoolDistributionVO;
import com.crm.service.cluePoolManager.CluePoolDistributionDbService;
import com.crm.system.service.cluePoolManager.CluePoolDistributionService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author luojie
 * @date 2020/12/26 15:29
 */
@Service
public class CluePoolDistributionServiceImpl implements CluePoolDistributionService {

    @Autowired
    private CluePoolDistributionDbService cluePoolDistributionDbService;

    @Override
    public Boolean addCluePoolDistribution(List<JSONObject> jsonObjects, String cluePoolId) {
        jsonObjects.stream().forEach(distribution-> {
            //如果分配人id不为空则添加
            if(!StringUtils.isBlank(distribution.getString("peopleId"))){
                UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
                CluePoolDistribution cluePoolDistribution = new CluePoolDistribution();
                cluePoolDistribution.setCluePoolDistributionId(CommonUtils.getGUID());
                cluePoolDistribution.setCompanyId(distribution.getString("companyId"));
                cluePoolDistribution.setPeopleId(distribution.getString("peopleId"));
                cluePoolDistribution.setCluePoolId(cluePoolId);
                cluePoolDistribution.setDeleteStatus(Short.parseShort(String.valueOf(StatusCodeEnum.NORMAL_CODE.getState())));
                cluePoolDistribution.setCreatePeopleId(userAccount.getAccountId());
                cluePoolDistribution.setCreateTm(new Date());
                cluePoolDistribution.setUpdatePeopleId(userAccount.getAccountId());
                cluePoolDistribution.setUpdateTm(new Date());
                cluePoolDistributionDbService.addCluePoolDistribution(cluePoolDistribution);
            }
        });
        return true;
    }

    @Override
    public Boolean deleteCluePoolDistributionByCluePoolId(String cluePoolId) {
        return cluePoolDistributionDbService.deleteCluePoolDistributionByCluePoolId(cluePoolId);
    }

    @Override
    public List<CluePoolDistributionVO> selectCluePoolDistributionVOList(String cluePoolId, Short deleteStatus) {
        return cluePoolDistributionDbService.selectCluePoolDistributionVOList(cluePoolId,deleteStatus);
    }
}
